﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace ReflectionUtilHelpV2
{
    public class ReflectionToolV2
    {
        public static IDictionary<string, string> GetDisplayName<T>() where T : class
        {
            try
            {
                IDictionary<string, string> _fields = new Dictionary<string, string>();
                PropertyInfo[] _properties = typeof(T).GetProperties();
                int _properityCnt = _properties.Length;
                foreach (PropertyInfo property in _properties)
                {
                    object[] _attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), false);
                    _fields.Add(property.Name, _attribute.Length == 0 ? property.Name : ((DisplayNameAttribute)_attribute[0]).DisplayName);
                }
                return _fields;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("GetDisplayName Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
